package com.ywgt.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ywgt.pojo.Goods;
import com.ywgt.pojo.Topic;
import com.ywgt.service.GoodsService;
import com.ywgt.service.TopicService;
import com.ywgt.util.ResponseUtil;
import io.swagger.annotations.ApiModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@CrossOrigin
@RestController
@RequestMapping("/wx/topic")
@ApiModel("主题")
public class TopicController {

    @Autowired
    TopicService topicService;

    @Autowired
    GoodsService goodsService;

    @GetMapping("/detail")
    public Object detail(Integer id){
        Map<String,Object> data=new HashMap<>();
        Topic topic = topicService.getById(id);
        List<Integer> ids=new ArrayList<>();
        String[] goodsId = topic.getGoods();
        List<Goods> goods=new ArrayList<>();
        if(goodsId!=null&&goodsId.length>0){
            Arrays.asList(goodsId).stream().forEach(a->ids.add(Integer.parseInt(a)));
            goods = goodsService.listByIds(ids);
        }
        data.put("goods",goods);
        return ResponseUtil.ok(data);
    }

    @GetMapping("/related")
    public Object related(Integer id){
        LambdaQueryWrapper<Topic> wrapper=new LambdaQueryWrapper<>();
        wrapper.ne(Topic::getId,id);
        List<Topic> topicRelatedList=topicService.list(wrapper);
        return ResponseUtil.ok(topicRelatedList);
    }
}
